<template>
    <div class="container">
        <el-button type="primary" @click="openDialog" class="add-btn">添加</el-button>
        <el-table :data="groups" border stripe class="group-table" height="calc(100vh - 220px)">
            <el-table-column prop="groupName" label="组名称" min-width="200" align="center" />
            <el-table-column label="操作" align="center" min-width="120">
                <template #default="scope">
                    <el-button type="danger" size="small" @click="removeGroup(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-dialog v-model="dialogVisible" title="添加号码组" width="320px" @close="resetDialog">
            <el-form :model="form" :rules="rules" ref="formRef" label-width="70px">
                <el-form-item label="组名称" prop="groupName">
                    <el-input v-model="form.groupName" maxlength="20" show-word-limit placeholder="请输入组名称" />
                </el-form-item>
            </el-form>
            <template #footer>
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="submitForm">确定</el-button>
            </template>
        </el-dialog>
    </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'

import { getGroup, addGroup, deleteGroup } from '@/api/Omission/group'

const groups = ref([])

const dialogVisible = ref(false)
const form = reactive({ groupName: '' })
const formRef = ref()
const rules = {
    groupName: [
        { required: true, message: '请输入组名称', trigger: 'blur' },
        { min: 1, max: 20, message: '长度1-20个字符', trigger: 'blur' }
    ]
}

const init = async () => {
    const res = await getGroup()
    if (res.code === 'S') {
        groups.value = res.data || []
    } else {
        ElMessage.error(res.message || '获取号码组列表失败')
    }
}
init()

const openDialog = () => {
    dialogVisible.value = true
}

const resetDialog = async () => {
    form.name = ''
    formRef.value && formRef.value.clearValidate()
    dialogVisible.value = false
}

const submitForm = () => {
    formRef.value.validate(async (valid) => {
        if (valid) {
            const res = await addGroup({ groupName: form.groupName })
            if (res.code === 'S') {
                ElMessage.success('添加成功')
                dialogVisible.value = false
                await resetDialog()
                await init()
            }
        } else {
            ElMessage.error('添加失败')
        }
    })
}

const removeGroup = async (row) => {
    const res = await deleteGroup(row)
    if (res.code !== 'S') {
        ElMessage.error(res.message || '删除失败')
        return
    }
    ElMessage.success('删除成功')
    await init()
}
</script>

<style scoped lang="scss">
@use "@/assets/global.scss" as *;

.group-container {
    padding: 24px 16px;
    background: #fff;
    border-radius: 8px;
    min-height: 300px;
}

.add-btn {
    margin-bottom: 18px;
}

.group-table {
    background: #fcfcfc;
    border-radius: 6px;
    box-shadow: 0 2px 8px 0 rgba(0, 0, 0, 0.03);
}
</style>